import $ from 'jquery'
import '../assets/style/content_script.less'
import {getParam} from '../utils/Tools'
import { postAccount } from '../axios/other'

$(document).ready(function(){
console.log('------------------执行插件------------------------')
///       判断是否在登录页
chrome.storage.sync.get("authorization", res => {
  if(location.host == 'mp.weixin.qq.com' && !res.authorization){
    return
  }
});

setTimeout(() => {
if($('#header .login_frame').length > 0){
  $('#header .login_frame .weui-desktop-global__info').after(`<span class="quick-login">快捷登录<img src="https://openwhite.oss-cn-guangzhou.aliyuncs.com/images/97abb8f89acd46169e4e95a302b9fa48.svg"></span>`)
  $('.quick-login').click(res=>{
    console.log('点击了快捷登录')
  })


  ////      切换到账号登录
  document.querySelector('.login__type__container__scan .login__type__container__select-type').click()    

  chrome.runtime.sendMessage({
    type: 'getAccount'
  },res=>{
    autoLogin(res)
  })

  $('.login_frame .btn_login').click(res=>{   ///  点击了登录
    let username = $("input[name='account']").val();
    let password = $("input[name='password']").val();
    // 保存账号和密码
    if (username && password) {
      // 开始记录账号
      handleSaveAccount({
        username,
        password,
      });
    }
  })    
}

///   登录成功后
if(location.pathname == '/cgi-bin/home' && getParam(location.search,'token')){
  chrome.runtime.sendMessage({
    type:'postAccount'
  },res=>{
    console.log('-------------------',res)
    if(res.username){
      chrome.runtime.sendMessage({
        type:'saveToken',
        data:{
          username: res.username,
          token:getParam(location.search,'token'),
          cookie:res.cookie
        }
      })
    }
    // postAccount({
    //   token:getParam(location.search,'token'),
    //   cookie:res
    // }).then(ret=>{
    //   chrome.runtime.sendMessage({
    //     type:'saveToken',
    //     data:{
    //       username: ret.data.email,
    //       token:getParam(location.search,'token'),
    //       cookie:res
    //     }
    //   })
    // })
  })
}

// 退出公众号
$("body").on("click", ".wxcloseTab", function() {
  chrome.runtime.sendMessage({ action: "close_tab" });
});

}, 1000);
})

///      处理保存账号
function handleSaveAccount(data){
  chrome.runtime.sendMessage({
    type: 'saveAccount',
    data
  })
}



///       自动填写账号密码
function autoLogin(res){
  const user = document.querySelector("input[name='account']");
  const pass = document.querySelector("input[name='password']");
  const event = document.createEvent("HTMLEvents");
  event.initEvent("input", false, true);
  user.value = res.username || ''; // 修改用户名输入框的值
  user.dispatchEvent(event); // 手动触发输入框的input事件
  pass.value = res.password || ''; // 修改密码输入框的值
  pass.dispatchEvent(event); // 手动触发输入框的input事件
  setTimeout(() => {
    if(res.username && res.password){
      document.querySelector('.login_btn_panel .btn_login').click()
      handleSaveAccount({
        username: res.username,
        password: res.password,
      });
    }
  }, 100);
}
